package cn.itsource.pethome.pay.controller;

import cn.itsource.pethome.pay.config.AlipayConfig;
import cn.itsource.pethome.pay.constant.PayConstants;
import cn.itsource.pethome.pay.domain.AlipayInfo;
import cn.itsource.pethome.pay.domain.PayBill;
import cn.itsource.pethome.pay.mapper.AlipayInfoMapper;
import cn.itsource.pethome.pay.mapper.PayBillMapper;
import cn.itsource.pethome.pay.service.IAlipayInfoService;
import cn.itsource.pethome.pay.service.IPayBillService;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController
@RequestMapping("/ali")
public class AlipayController {
    //支付订单
    @Autowired
    private IPayBillService payBillService;
    //支付信息
    @Autowired
    private IAlipayInfoService alipayInfoService;


    /**
     * 异步请求
     * @param request
     */
    @PostMapping("/notify")
    public String notifyRequest(HttpServletRequest request){

        try {
            //获取支付宝POST过来反馈信息
            Map<String,String> params = new HashMap<String,String>();
            Map<String,String[]> requestParams = request.getParameterMap();
            for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i]
                            : valueStr + values[i] + ",";
                }
                params.put(name, valueStr);
            }
            //商户订单号
            String out_trade_no = request.getParameter("out_trade_no");
            //通过订单号查询支付支付订单
            AlipayInfo alipayInfo = alipayInfoService.loadByOrderSn(out_trade_no);
            Boolean signVerified = false;
            if (alipayInfo != null){
                //调用SDK验证签名
                signVerified = AlipaySignature.rsaCheckV1(params, alipayInfo.getAlipay_public_key(), AlipayConfig.charset, AlipayConfig.sign_type);
            }
            if(signVerified) {//验证成功
                //支付宝交易号
                String trade_no = request.getParameter("trade_no");
                //交易状态
                String trade_status = request.getParameter("trade_status");
                if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){
                    //处理订单状态(服务订单，支付订单)
                    return  payBillService.handleOrder(out_trade_no, trade_no);
                }
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "fail";
    }
}
